package top.lilele.adminSystem.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import top.lilele.adminSystem.entity.AdminUserEntity;
import top.lilele.adminSystem.mapper.AdminUserMapper;
import top.lilele.adminSystem.pojo.RedisPojo;
import top.lilele.adminSystem.utils.RedisUtil;
import top.lilele.adminSystem.utils.Utils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.List;

/**
 * 应用程序启动读取所有用户信息
 *
 * @author lilele
 */
@Slf4j
@Component
public class ApplicationStartListener implements ServletContextAware {
    @Resource
    AdminUserMapper adminUserMapper;
    @Resource
    RedisUtil redisUtil;

    @Override
    public void setServletContext(ServletContext servletContext) {
        log.warn("应用程序启动，正在读取授权信息到redis");
        List<AdminUserEntity> list = adminUserMapper.selectList(null);
        redisUtil.set(Utils.getName(RedisPojo::getUserList), list);
    }
}
